package com.itheima.controller;

import com.itheima.advice.LogOperation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.impl.DeptServiceImpl;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@Slf4j
//@Scope("prototype")多例测试
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptServiceImpl deptService;

    @GetMapping
    public Object findAll(){
        log.info("执行查询方法");
       List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    @DeleteMapping
    @LogOperation
    public  Result deleteById(@RequestParam("id") Integer id){
        log.info("执行删除方法");
        deptService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    @LogOperation
    public Result saveDept(@RequestBody Dept dept){
        log.info("进入了添加方法,参数是"+ dept);
        deptService.saveDept(dept);
        return Result.success();
    }

//    @GetMapping("/{id}")
//    public Result findById(@PathVariable("id") Integer id){
//        System.out.println("进入了按id查找方法");
//        Dept dept = deptService.findById(id);
//        return Result.success(dept);
//
//    }



@GetMapping("/{id}")
public Result getById(@PathVariable Integer id){
        log.info("执行了按id查找");
    Dept dept = deptService.foundById(id);
    return Result.success(dept);
}

@PutMapping
@LogOperation
public Result update(@RequestBody Dept dept){
        log.info("执行了更新方法");
        deptService.update(dept);
        return Result.success();
}

//    @PutMapping("/depts")
//    public Result update(@RequestBody Dept dept){
//        System.out.println("进入了更新方法");
//        deptService.update(dept);
//        return Result.success();
//    }

}

